package com.vcredit.creditcat.manager.product;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.api.IXyhManager;
import com.vcredit.creditcat.dto.product.CreditCRFAmountParamDto;
import com.vcredit.creditcat.enumtype.MerchantProductEnum;
import com.vcredit.creditcat.enumtype.SmsDescEnum;
import com.vcredit.creditcat.manager.SMSMessageManager;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.JacksonUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class XyhProductManager implements IXyhManager {



	@Autowired
	private CustProductApplyService custProductApplyService;
	@Autowired
	private CustService custService;
	@Autowired
	private ProductApplyManager productApplyManager;
	
	@Autowired
	private CustAccountService custAccountService;
	
	@Autowired
	public SMSMessageManager sMSMessageManager;

	@Log
	@Override
	public void dealResult(String productName, String jsonParam) throws Exception {		
		productApplyManager.noticeApplyResult(this.productApply(productName, jsonParam.toString()), productName);
	}

	/**
	 * 所需参数封装
	 * 
	 * @param productName
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	public CustProductApply productApply(String productName, String jsonParam) throws Exception {
		//ApiEnum enumOnValue = EnumExtUtil.getEnumOnValue(ApiEnum.class, productName, "key");
		CustProductApply cust = new CustProductApply();
		cust = handleFlowerResult(jsonParam);		
		return cust;
	}
	@Log
	public CustProductApply handleFlowerResult(String jsonEntry) throws Exception {
		CreditCRFAmountParamDto result = JacksonUtil.Json2Obj(jsonEntry, new TypeReference<CreditCRFAmountParamDto>() {
		});
		String idNo = result.getIdNo();
		Cust cust = custService.findByIdNo(idNo);
	
		
		CustProductApply custProductApply = new CustProductApply();
		custProductApply.setCustId(cust.getCustId());
		custProductApply.setProductId(new Long(MerchantProductEnum.creditFlower.getCode()));
		List<CustProductApply> list = custProductApplyService.findByConditions(custProductApply);
		if (list.size() > 0) {
			custProductApply.setApplyId(list.get(0).getApplyId());
		}
		String flowNo = Utility.nextCode(MerchantProductEnum.creditFlower.name().toUpperCase());
		custProductApply.setFlowNo(flowNo);
		
		
		
		
		// 处理授信状态
		String creditStatus = result.getOrderState();
		if(creditStatus==null){
			throw new Exception("信用花回调返回状态异常");
		}
		switch (creditStatus) {
		case "2": // 拒绝授信
			custProductApply.setAuditResult(0);
			if(result.getRemark()!=null){
				custProductApply.setRejectMsg("信用花拒绝："+result.getRemark());
			}else{
				custProductApply.setRejectMsg("信用花拒绝授信");
			}			
			custProductApply.setIsApply(true);
			custProductApply.setCreditApplyTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(),30)));
			
			try {  //发送失败短信，不影响主流程
				CustAccount account = new CustAccount();
				account.setCustId(cust.getCustId());
				CustAccount  custAccount=custAccountService.find(account).get(0);
				
				String mobile = custAccount.getUserName();
				String appdesc=custAccount.getAppDesc();	
				appdesc=appdesc.split(";")[0].toString();	
				String productName=SmsDescEnum.getNameByAppDesc(appdesc);
				String userName=custAccount.getAccountRealname();

				sMSMessageManager.sendCreditFail(mobile,productName,userName);
			} catch (Exception e) {
				
			}
			return custProductApply;
		case "4": // 已完成授信
			custProductApply.setAuditResult(1);
			custProductApply.setIsApply(false);
			custProductApply.setCreditAmt(result.getCreditAmt() == null ? 0 : result.getCreditAmt());
			custProductApply.setCreditExpiretime(result.getCreditExpiretime()+" 23:59:59");
			custProductApply.setServiceRate(result.getServiceRate());
			custProductApply.setTotalServiceRate(result.getTotalServiceRate());
			custProductApply.setFormalitiesRate(result.getFormalitiesRate());
			custProductApply.setInterestRateMonthly(result.getInterestRateMonthly());
			custProductApply.setRejectMsg("授信成功");
			
			try { //发送成功短信，不影响主流程
				CustAccount account = new CustAccount();
				account.setCustId(cust.getCustId());
				CustAccount  custAccount=custAccountService.find(account).get(0);
								
				String mobile = custAccount.getUserName();
				String appdesc=custAccount.getAppDesc();
				appdesc=appdesc.split(";")[0].toString();	
				String productName=SmsDescEnum.getNameByAppDesc(appdesc);
				String userName=custAccount.getAccountRealname();
				
				sMSMessageManager.sendCreditSuccess(mobile,productName,userName);
			} catch (Exception e) {
				
			}
						
			return custProductApply;
		case "5": // 授信中
			custProductApply.setAuditResult(2);
			custProductApply.setRejectMsg("信用花授信中");
			custProductApply.setIsApply(false);
			return custProductApply;
			
		case "7": // 信用补救，可重新授信
			custProductApply.setAuditResult(7);
			custProductApply.setRejectMsg("信用花信用补救");
			custProductApply.setIsApply(false);
			return custProductApply;
		default: 
			return custProductApply;
		}
	}
		
}
